package org.jiuling.aliyun.sms.factory.service.impl;

import com.aliyun.dysmsapi20170525.models.QuerySendDetailsRequest;
import com.aliyun.dysmsapi20170525.models.QuerySendStatisticsRequest;
import org.jiuling.aliyun.sms.config.AliYunSmsConfig;
import org.jiuling.aliyun.sms.convert.QuerySendDetailsResponseConvert;
import org.jiuling.aliyun.sms.convert.QuerySendStatisticsResponseConvert;
import org.jiuling.aliyun.sms.enums.SmsSendRangeEnum;
import org.jiuling.aliyun.sms.enums.TemplateTypeEnum;
import org.jiuling.aliyun.sms.exception.AliYunSMSRuntimeException;
import org.jiuling.aliyun.sms.factory.AliYunSmsSynchronization;
import org.jiuling.aliyun.sms.factory.service.AliYunSendSmsQueryService;
import org.jiuling.aliyun.sms.factory.service.verify.AliYunSendSmsQueryVerify;
import org.jiuling.aliyun.sms.model.QuerySendDetailsResponseModel;
import org.jiuling.aliyun.sms.model.QuerySendStatisticsResponseModel;
import org.jiuling.basics.converter.DateConvert;
import org.jiuling.basics.enums.DateTimeFormatEnum;
import org.jiuling.basics.validation.ObjectVerify;

import java.util.Date;

/**
 * 阿里云同步发送短信查询服务实现
 * @author 紫灵
 * @date 2023年02月10日 15时58分35秒
 */
public class AliYunSyncSendSmsQueryServiceImpl extends AliYunSmsSynchronization implements AliYunSendSmsQueryService {


    public AliYunSyncSendSmsQueryServiceImpl(AliYunSmsConfig aliYunSmsConfig) {
        super(aliYunSmsConfig);
    }

    @Override
    public QuerySendStatisticsResponseModel querySendStatistics(String signName,SmsSendRangeEnum smsSendRange, String startDate, String endDate, TemplateTypeEnum templateType, int pageNum, int pageSize) {
        // 校验查询短信统计信息参数
        AliYunSendSmsQueryVerify.querySendStatisticsParam(smsSendRange, startDate, endDate);
        // 构建查询短信统计信息请求
        QuerySendStatisticsRequest querySendStatisticsRequest = new QuerySendStatisticsRequest()
                .setIsGlobe(smsSendRange.getValue())
                .setStartDate(startDate)
                .setEndDate(endDate)
                .setSignName(signName)
                .setPageIndex(pageNum)
                .setPageSize(pageSize);
        // 判断模板类型是否为空
        if(!ObjectVerify.isNull(templateType)){
            querySendStatisticsRequest.setTemplateType(templateType.getValue());
        }
        try {
            return QuerySendStatisticsResponseConvert.bodyToModel(create()
                    .querySendStatistics(querySendStatisticsRequest).getBody());
        } catch (Exception e) {
            throw new AliYunSMSRuntimeException(e.getLocalizedMessage());
        }
    }

    @Override
    public QuerySendDetailsResponseModel querySendDetails(String phoneNumber, String bizId, Date sendDate, long pageNum, long pageSize) {
        AliYunSendSmsQueryVerify.querySendDetailsParam(phoneNumber, sendDate);
        QuerySendDetailsRequest querySendDetailsRequest = new QuerySendDetailsRequest()
                .setPhoneNumber(phoneNumber)
                .setBizId(bizId)
                .setSendDate(DateConvert.dateToString(sendDate, DateTimeFormatEnum.yyyyMMdd))
                .setPageSize(pageSize)
                .setCurrentPage(pageNum);
        try {
            return QuerySendDetailsResponseConvert.bodyToModel(create()
                    .querySendDetails(querySendDetailsRequest).getBody());
        }catch (Exception e){
            throw new AliYunSMSRuntimeException(e.getLocalizedMessage());
        }
    }
}
